﻿@inherits NITASA.Views.AutofacEnabledViewPage<NITASA.Data.Content>
@using NITASA.Services.Security;
@using NITASA.Data
@using NITASA.Areas.Admin.Helper
@{
    ViewBag.Title = "New Addon";
}
@section PageLevelStyles{
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <style type="text/css">
        strong {
            font-weight: 600;
        }
    </style>
    <script src="@Url.Content("~/Areas/Admin/assets/js/tinymce/tinymce.min.js")"></script>
    <script type="text/javascript">

        $ext = 'span[id|name|class|style]';
        tinyMCE.init({
            selector: ".edtr",
            theme: "modern",
            menubar: false,
            plugins: [
                "advlist autolink lists link image charmap preview hr anchor",
                "searchreplace wordcount visualblocks visualchars code fullscreen",
                "insertdatetime nonbreaking save table contextmenu directionality",
                "emoticons template paste textcolor colorpicker textpattern "
            ],
            toolbar1: "undo redo | styleselect | forecolor backcolor | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link | Variables | mediaimages | code | Addons",
            image_advtab: false,
            extended_valid_elements: $ext,
            force_br_newlines: false,
            force_p_newlines: false,
            forced_root_block: '',
            setup: function (editor) {
                editor.addButton('mediaimages', {
                    text: '',
                    title: 'Insert image',
                    icon: 'mce-ico mce-i-image',
                    onclick: function () {
                        $("#MediaPopup").modal('show');
                    }
                });
            }
        });
    </script>
}
<div class="main-content">
    <div class="container-fluid padded">
        @if (TempData["ErrorMessage"] != null)
        {
            <div class="alert alert-error">
                <button class="close" data-dismiss="alert" type="button">
                    ×
                </button>
                @TempData["ErrorMessage"]
            </div>
        }
        <div id="containerfluid"></div>
        <div class="row-fluid">
            <div class="span12">
                <div class="box">
                    <div class="box-header">
                        <span class="title"><i class="icon-book"></i>&nbsp; New Addon</span> @*<ul class="box-toolbar">
                                <li>
                                    <a href="@Url.Action("List", "Post")" title="Back to list" ><i class="icon-reply" style="margin-top: 10px"></i></a>
                                </li>
                            </ul>*@
                    </div>
                    <div class="box-content">
                        <div class="padded">
                            <div class="span12">
                                @using (Html.BeginForm("Add", "Addons", FormMethod.Post, new { id = "frm1" }))
                                {
                                    <div class="span9">
                                        <div class="control-group">
                                            <div class="controls">
                                                @Html.TextBoxFor(mbox => mbox.Type, new { placeholder = "Addon Name", @class = "width100per", style = "margin-bottom: 0px;", id = "txtAddonName" })
                                                <div style="color: red">
                                                    @Html.ValidationMessageFor(m => m.Type)
                                                </div>
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <div class="controls">
                                                <input type="hidden" id="hdnIsPost" name="hdnIsPost" />
                                                @Html.TextBoxFor(mbox => mbox.Title, new { placeholder = "Addon Title", @class = "width100per", style = "margin-bottom: 0px;" })
                                                <div style="color: red">
                                                    @Html.ValidationMessageFor(m => m.Title)
                                                </div>
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <div class="controls">
                                                @Html.TextBoxFor(m => m.URL, new { placeholder = "Addon URL", @class = "width100per", style = "margin-bottom: 0px;" })
                                                <div style="color: red">
                                                    @Html.ValidationMessageFor(m => m.URL)
                                                </div>
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <div class="controls">
                                                <label>
                                                    <b>Content: </b>
                                                </label>
                                                <div>
                                                    @Html.TextAreaFor(m => m.Description, new { @class = "edtr", style = "width: 100%; max-width: 98%; height:525px; margin: 0px; overflow: hidden; word-wrap: break-word;resize:vertical;" })
                                                    @*@class = "textarea-html5", *@
                                                    <div style="color: red">
                                                        @Html.ValidationMessageFor(m => m.Description)
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="span3">
                                        <div class="padded" style="padding: 0px 4px 13px 4px; ">
                                            @if (aclService.HasRight(Rights.PublishOwnAddons))
                                            {
                                                <input type="button" class="btn btn-small btn-green" id="btnPost" style="text-align: center; width: 60px;" value="Publish" />
                                            }&nbsp;
                                            <input type="button" class="btn btn-small btn-blue" style="text-align: center; width: 90px;" value="Save to Draft" id="btnSaveToDraft" />&nbsp;
                                            <input type="button" class="btn btn-small btn-red" style="text-align: center; width: 60px;" value="Back" onclick="window.location = '/Admin/Addons/List';" />
                                        </div>
                                        <div class="box">
                                            <div class="box-header">
                                                <span class="title">Display Order</span>
                                            </div>
                                            <div class="box-content">
                                                <div class="padded" style="text-align:left;vertical-align:top">
                                                    @Html.TextBoxFor(mbox => mbox.ContentOrder, new { placeholder = "Addon Order To Display", style = "margin-bottom: 0px;" })
                                                    <div style="color: red">
                                                        @Html.ValidationMessageFor(m => m.ContentOrder)
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div> }
                            </div>
                            &nbsp;
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@{Html.RenderAction("GetMedia", "Media", new { isEditor = true });}

@section BottomScript{
    <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            $('#btnPost').click(function () {
                $("#hdnIsPost").val("Post");
                tinyMCE.triggerSave();
                validpost();
            });

            $('#btnSaveToDraft').click(function () {
                $("#hdnIsPost").val("SaveToDraft");
                tinyMCE.triggerSave();
                validpost();
            });
            function validpost() {
                if ($("#frm1").valid()) {
                    $('#frm1').submit();
                }
                else {
                    var error = '<div class="alert alert-error"><button class="close" data-dismiss="alert" type="button">×</button>Please solve form errors</div>';
                    $("#containerfluid").html(error);
                }
            }
            $("body").delegate(".wizard-input-section .media-thumb .imgSelect", "click", function () {
                var imgPath = $(this).attr('src');
                var url = imgPath;
                var img = "<img src=\"" + url + "\"/>";
                tinyMCE.activeEditor.execCommand('mceInsertContent', false, img);
                $("#MediaPopup").modal("hide");
            });

        });

        $(function () {
            $("#txtAddonName").autocomplete({
                source: '@Url.Action("GetAddonNames", "Addons")',
                minLength: 1
            });
        });

    </script>
}
